package com.shrw.duke.serialport.shengbenserial.port;

import android.util.Log;

import com.odm.misc.MiscDevice;
import com.odm.tty.TtyDevice;
import com.shrw.duke.serialport.shengbenserial.runnable.MyRunnable;
import com.shrw.duke.serialport.utils.PortDataTrimUtils;


public class BasewinPort extends Port {

//	private static final Logger logger = LoggerFactory.getLogger(BasewinPort.class);
	
	private static final String TAG = "BasewinPort";
	ReceiveThread thread = null;
	private PortDataTrimUtils trimUtils;

	private static final int READ_BUF_LENGTH = 1024;
	private MiscDevice mMiscDevice;
	private TtyDevice mTtyDevice;
	
	private static final String TTY_DEV = "/dev/ttyMSM2";///dev/ttyMSM2
	private static final String MISC_DEV = "/dev/SE955_MISC";// 上电设备
//	private static final String MISC_DEV = "/dev/DEV_M3CCD";// 上电设备
	private static final int MISC_IO = 0xA3;

	private static BasewinPort instance = null;
	
	private boolean isStarted = false;

	private BasewinPort() {
		mMiscDevice = new MiscDevice(MISC_DEV, MISC_IO);
		mTtyDevice = new TtyDevice(TTY_DEV);
		trimUtils = new PortDataTrimUtils();
	}

	public static BasewinPort getInstance() {
		if (instance == null) {
			synchronized (BasewinPort.class){
				if (instance == null){
					instance = new BasewinPort();
				}
			}
		}
		return instance;
	}

	@Override
	public void finalize() {
		thread.stop();
		devicePowerOff();
		deviceClose();
	}

	public void deviceOpen() {
		mTtyDevice.ttyOpen();
//		mTtyDevice.ttyInit(TtyDevice.TTY_B38400);
		mTtyDevice.ttyInit(TtyDevice.TTY_B115200);
	}

	public boolean isOpen(){
		return mTtyDevice.isOpened();
	}

	public void deviceClose() {
		mTtyDevice.ttyClose();
	}

	public boolean sendCommd(byte[] cmd) {
		int writeRet = 0;
		writeRet = mTtyDevice.ttyWrite(cmd);
		return writeRet == cmd.length;
	}
	
	public void devicePowerOn() {
		mMiscDevice.setPinHigh(0);
		mMiscDevice.setPinHigh(1);
//		mMiscDevice.setPinHigh(2);
//		mMiscDevice.setPinHigh(3);
//		mMiscDevice.setPinHigh(4);
//		mMiscDevice.setPinHigh(5);
//		mMiscDevice.setPinHigh(6);
	}

	public void devicePowerOff() {
		mMiscDevice.setPinLow(0);
		mMiscDevice.setPinLow(1);
//		mMiscDevice.setPinLow(2);
//		mMiscDevice.setPinLow(3);
//		mMiscDevice.setPinLow(4);
//		mMiscDevice.setPinLow(5);
//		mMiscDevice.setPinLow(6);
	}

	class ReceiveThread extends MyRunnable {
		
		protected ReceiveThread(int parmTime) {
			super(parmTime);
		}

		@Override
		protected void doUnitOfWork() {
			try{
				byte[] readBuf = new byte[READ_BUF_LENGTH];
				int len = mTtyDevice.ttyRead(readBuf);
				if (len > 0) {
					byte[] tempBuf = new byte[len];
					System.arraycopy(readBuf, 0, tempBuf, 0, len);
					trimUtils.add(tempBuf,len);
					tempBuf = trimUtils.get();
					if (tempBuf!=null){
						callReadReturnDataListenerOnReported(tempBuf);
					}
//					Log.d(TAG, "len:" + String.valueOf(len));
//					logger.debug(HexUtils.bytes2HexString(tempBuf));
				}
			}catch(Exception ex){
//				logger.error(ex.getMessage());
			}
		}
	}

	@Override
	public void stop() {
		
		if(isStarted){
			thread.stop();
			thread = null;
			devicePowerOff();
			deviceClose();
			
			isStarted = false;
		}
	}

	@Override
	public void start() {
		if(!isStarted){
			deviceOpen();
			devicePowerOn();

			thread = new ReceiveThread(100);
			thread.start(true);
	 
			isStarted = true;
		}
	}
}
